//
//  SimpleNetWorkTests.swift
//  SimpleNetWorkTests
//
//  Created by mac on 15/3/2.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit
import XCTest

class SimpleNetWorkTests: XCTestCase {
    
    let urlString = "http://httpbin.org/get"
    let TIME_OUT: NSTimeInterval = 10                           // 超时时间
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    // MARK: 测试单例
    func testShareInstance(){
        let i = SimpleNetWork.shareInstance
        let b = SimpleNetWork.shareInstance
        XCTAssert(i === b, "测试单例失败")
    }
    
    // MARK: 测试获得请求参数字符串
    func testGetParamsString(){
        let queryStr1 = SimpleNetWork.shareInstance.queryString(nil)
        XCTAssertNil(queryStr1, "返回值应该为空")
        
        let queryStr2 = SimpleNetWork.shareInstance.queryString(["name": "zhangsan", "title": "hello"])
        XCTAssertEqual("title=hello&name=zhangsan", queryStr2!, "转化格式不正确")
        
        let queryStr3 = SimpleNetWork.shareInstance.queryString(["name": "zhangsan", "title": "hello", "book": "iOS 8.0"])
        XCTAssertEqual("title=hello&book=iOS%208.0&name=zhangsan", queryStr3!, "转化格式不正确")
    }
    
    // MARK: 测试 HTTP 请求的创建
    func testGetRequest() {
        let req1 = SimpleNetWork.shareInstance.requestFromURLString("", HTTPMethod.GET, nil)
        XCTAssertNil(req1, "请求应该为空")
        let req2 = SimpleNetWork.shareInstance.requestFromURLString(urlString, HTTPMethod.GET, "title=hello&book=iOS%208.0&name=zhangsan")
        XCTAssertNotNil(req2, "请求不应该为空")
        let str = req2!.URL!.absoluteString!
        XCTAssert(str == urlString + "?title=hello&book=iOS%208.0&name=zhangsan" , "GET 请求地址有误")
        let req3 = SimpleNetWork.shareInstance.requestFromURLString(urlString, HTTPMethod.POST, "title=hello&book=iOS%208.0&name=zhangsan")
        XCTAssertNotNil(req3, "请求不应该为空")
        let str3 = NSString(data: req3!.HTTPBody!, encoding: NSUTF8StringEncoding)
        XCTAssert(str3 == "title=hello&book=iOS%208.0&name=zhangsan", "请求参数有误")
    }
    
    // MARK: 测试发送请求
    func testHTTPRequest() {
        // 设置期望
        let exp1 = expectationWithDescription(urlString)
        SimpleNetWork.shareInstance.requestJSON(urlString, HTTPMethod.GET, nil) { (result, error) -> () in
            XCTAssert(NSThread.isMainThread(), "回调方法应该在主线程中执行")
            XCTAssertNil(error, "返回错误应该为为空")
            XCTAssertNotNil(result, "返回应该有结果信息")
            println("---\(result)")
            exp1.fulfill()
        }
        waitForExpectationsWithTimeout(TIME_OUT, handler: nil)
    }
    
    // MARK: 请求方法跟请求参数矛盾
    func errorHTTPMethod() {
        // 设置期望
        let exp1 = expectationWithDescription(urlString)
        SimpleNetWork.shareInstance.requestJSON(urlString, HTTPMethod.POST, nil) { (result, error) -> () in
            XCTAssert(NSThread.isMainThread(), "回调方法应该在主线程中执行")
            XCTAssertNil(result, "返回结果应该为为空")
            XCTAssertNotNil(error, "返回应该有错误信息")
            println("---\(error)")
            exp1.fulfill()
        }
        waitForExpectationsWithTimeout(TIME_OUT, handler: nil)
    }
    
    // MARK: 空地址
    func emptyURLRequest(){
        // 设置期望
        let exp1 = expectationWithDescription("空地址")
        SimpleNetWork.shareInstance.requestJSON("", HTTPMethod.POST, nil) { (result, error) -> () in
            XCTAssert(NSThread.isMainThread(), "回调方法应该在主线程中执行")
            XCTAssertNil(result, "返回结果应该为空")
            XCTAssertNotNil(error, "返回应该有错误信息")
            exp1.fulfill()
        }
        waitForExpectationsWithTimeout(TIME_OUT, handler: nil)
    }
}
